package com.jt.common.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RegisterTests {
    //存储服务信息的容器
    //private static Map<String,String> map = new HashMap<>();
    //ConcurrentHashMap<>()是线程安全对象
    private static Map<String,String> map = new ConcurrentHashMap<>();
    //获取读写锁 在MappingRegister类中
    private static final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    //注册服务
    public static void doRegister(String serviceId,String host){
        //读写锁--写锁（悲观锁） 在同一时间只能一个写
        readWriteLock.writeLock().lock();
        try {
            map.put(serviceId, host);
        }finally {
            readWriteLock.writeLock().unlock();
        }

    }
    //发现服务 通过get（key）获取
    public static String doLookup(String serviceId){
        //读写锁-读锁（乐观读） 别人在写的时候能读 别人读的时候也能读-乐观读
        readWriteLock.readLock().lock();
        try{
            return map.get(serviceId);
        }finally {
            readWriteLock.readLock().unlock();
        }

    }
    public static void main(String[] args) {
       //服务名一地址一一对应
        //String[] serviceIds = {"service-A","service-B","service-C","service-D"};
        //String[] hosts = {"localhost:8081","localhost:8082","localhost:8083","localhost:8084"};
        String[] serviceId = new String[100];
        String[] hosts = new String[100];
        for (int i = 0; i < 100; i++) {
            serviceId[i] = "serviceId-" + i;
            hosts[i] = "host-" + i;
        }
/*        ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < serviceIds.length; i++) {
                    doRegister(serviceIds[i], hosts[i]);
                    String s = doLookup(serviceIds[i]);
                    System.out.println(serviceIds[i] + "->" + s);
                }
            }
        };
        service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);*/
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 25; i++) {
                    doRegister(serviceId[i],hosts[i]);
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 25; i < 50; i++) {
                    doRegister(serviceId[i],hosts[i]);
                }
            }
        };
        Thread t3 = new Thread(){
            @Override
            public void run() {
                for (int i = 50; i < 75; i++) {
                    doRegister(serviceId[i],hosts[i]);
                }
            }
        };
        Thread t4 = new Thread(){
            @Override
            public void run() {
                for (int i = 75; i < 100; i++) {
                    doRegister(serviceId[i],hosts[i]);
                }
            }
        };
        t1.start();
        t2.start();
    }
}
